from sqlalchemy import Column, String, Text, text,Index,Integer,SmallInteger,Boolean
from app.db.base_class import Base

__all__ = [
    'SystemConfig', 'SystemAuthAdmin', 'SystemAuthMenu', 'SystemAuthPerm', 'SystemAuthRole', 'SystemLogLogin',
    'SystemLogOperate', 'SystemAuthDept', 'SystemAuthPost', "SystemLogSms",
    'system_config', 'system_auth_admin', 'system_auth_menu', 'system_auth_perm', 'system_auth_role',
    'system_log_login', 'system_log_operate', 'system_auth_post', 'system_auth_dept',
    "system_log_sms"
]


class SystemConfig(Base):
    """系统配置实体"""
    __tablename__ = 'la_system_config'
    __table_args__ = ({'comment': '系统全局配置表'})

    type = Column(String(30), nullable=True, default='', comment='类型')
    name = Column(String(60), nullable=False, default='', comment='键')
    value = Column(Text, nullable=True, comment='值')

class SystemAuthAdmin(Base):
    """系统管理员实体"""
    __tablename__ = 'la_system_auth_admin'
    __table_args__ = ({'comment': '系统管理成员表'})


    # dept_id = Column(Integer, nullable=False, server_default=text('0'), comment='部门ID')
    # post_id = Column(Integer, nullable=False, server_default=text('0'), comment='岗位ID')
    username = Column(String(32), nullable=False, default='', comment='用户账号')
    nickname = Column(String(32), nullable=False, default='', comment='用户昵称')
    password = Column(String(200), nullable=False, default='', comment='用户密码')
    avatar = Column(String(200), nullable=False, default='', comment='用户头像')
    # role = Column(String(200), nullable=False, default='', comment='角色主键')
    role_ids = Column(String(200), nullable=False, default='', comment='角色主键')
    dept_ids = Column(String(200), nullable=False, default='', comment='部门ID')
    post_ids = Column(String(200), nullable=False, default='', comment='岗位ID')
    salt = Column(String(20), nullable=False, default='', comment='加密盐巴')
    sort = Column(SmallInteger, nullable=False, server_default=text('0'), comment='排序编号')
    is_multipoint = Column(Boolean, nullable=False, server_default=text('0'),
                           comment='多端登录: 0=否, 1=是')
    is_disable = Column(Boolean, nullable=False, server_default=text('0'),
                        comment='是否禁用: [0=否, 1=是]')
    last_login_ip = Column(String(30), nullable=False, default='', comment='最后登录IP')
    last_login_time = Column(Integer, nullable=False, server_default=text('0'),
                             comment='最后登录时间')


class SystemAuthMenu(Base):
    """系统菜单实体"""
    __tablename__ = 'la_system_auth_menu'
    __table_args__ = ({'comment': '系统菜单管理表'})

    pid = Column(Integer, nullable=False, server_default=text('0'), comment='上级菜单')
    menu_type = Column(String(16), nullable=False, default='', comment='权限类型: M=目录，C=菜单，A=按钮')
    menu_name = Column(String(100), nullable=False, default='', comment='菜单名称')
    menu_icon = Column(String(100), nullable=False, default='', comment='菜单图标')
    menu_sort = Column(SmallInteger, nullable=False, server_default=text('0'), comment='菜单排序')
    perms = Column(String(100), nullable=False, default='', comment='权限标识')
    paths = Column(String(100), nullable=False, default='', comment='路由地址')
    component = Column(String(200), nullable=False, default='', comment='前端组件')
    selected = Column(String(200), nullable=False, default='', comment='选中路径')
    params = Column(String(200), nullable=False, default='', comment='路由参数')
    is_cache = Column(SmallInteger, nullable=False, server_default=text('0'),comment='是否缓存: 0=否, 1=是')
    is_show = Column(SmallInteger, nullable=False, server_default=text('1'),comment='是否显示: 0=否, 1=是')
    is_disable = Column(SmallInteger, nullable=False, server_default=text('0'),comment='是否禁用: 0=否, 1=是')


class SystemAuthPerm(Base):
    """系统角色菜单实体"""
    __tablename__ = 'la_system_auth_perm'
    __table_args__ = ({'comment': '系统角色菜单表'})

    role_id = Column(Integer, nullable=False, server_default=text('0'), comment='角色ID')
    menu_id = Column(Integer, nullable=False, server_default=text('0'), comment='菜单ID')


class SystemAuthRole(Base):
    """系统角色实体"""
    __tablename__ = 'la_system_auth_role'
    __table_args__ = ({'comment': '系统角色管理表'})

    name = Column(String(100), nullable=False, default='', comment='角色名称')
    remark = Column(String(200), nullable=False, default='', comment='备注信息')
    is_disable = Column(Boolean, nullable=False, server_default=text('0'),
                        comment='是否禁用: 0=否, 1=是')
    sort = Column(SmallInteger, nullable=False, server_default=text('0'), comment='角色排序')


class SystemLogLogin(Base):
    """系统登录日志实体"""
    __tablename__ = 'la_system_log_login'
    __table_args__ = ({'comment': '系统登录日志表'})


    admin_id = Column(Integer, nullable=False, server_default=text('0'), comment='管理员ID')
    username = Column(String(32), nullable=False, default='', comment='登录账号')
    ip = Column(String(30), nullable=False, default='', comment='登录地址')
    os = Column(String(100), nullable=False, default='', comment='操作系统')
    browser = Column(String(100), nullable=False, default='', comment='浏览器')
    status = Column(SmallInteger, nullable=False, server_default=text('0'),
                    comment='操作状态: 1=成功, 2=失败')

class SystemLogOperate(Base):
    """系统操作日志实体"""
    __tablename__ = 'la_system_log_operate'
    __table_args__ = ({'comment': '系统操作日志表'})

    admin_id = Column(Integer, nullable=False, server_default=text('0'), comment='操作人ID')
    type = Column(String(30), nullable=False, default='', comment='请求类型: GET/POST/PUT')
    title = Column(String(30), default='', comment='操作标题')
    ip = Column(String(30), nullable=False, default='', comment='请求IP')
    url = Column(String(200), nullable=False, default='', comment='请求接口')
    method = Column(String(200), nullable=False, default='', comment='请求方法')
    args = Column(Text, comment='请求参数')
    error = Column(Text, comment='错误信息')
    status = Column(Boolean, nullable=False, server_default=text('1'),comment='执行状态: 1=成功, 2=失败')
    start_time = Column(Integer, nullable=False, server_default=text('0'), comment='开始时间')
    end_time = Column(Integer, nullable=False, server_default=text('0'), comment='结束时间')
    task_time = Column(Integer, nullable=False, server_default=text('0'), comment='执行耗时')


class SystemAuthDept(Base):
    __tablename__ = 'la_system_auth_dept'
    __table_args__ = ({'comment': '系统部门管理表'})

    pid = Column(Integer, nullable=False, server_default=text('0'), comment='上级主键')
    name = Column(String(100), nullable=False, default='', comment='部门名称')
    duty = Column(String(30), nullable=False, default='', comment='负责人名')
    mobile = Column(String(30), nullable=False, default='', comment='联系电话')
    sort = Column(SmallInteger, nullable=False, server_default=text('0'), comment='排序编号')
    is_stop = Column(Boolean, nullable=False, server_default=text('0'),
                     comment='是否停用: 0=否, 1=是')


class SystemAuthPost(Base):
    __tablename__ = 'la_system_auth_post'
    __table_args__ = ({ 'comment': '系统岗位管理表'})

    code = Column(String(30), nullable=False, server_default=text('0'), comment='岗位编码')
    name = Column(String(30), nullable=False, default='', comment='岗位名称')
    remarks = Column(String(250), nullable=False, default='', comment='岗位备注')
    sort = Column(SmallInteger, nullable=False, server_default=text('0'), comment='排序编号')
    is_stop = Column(SmallInteger, nullable=False, server_default=text('0'),comment='是否停用: 0=否, 1=是')

class SystemLogSms(Base):
    """
    系统短信日志表
    """
    __tablename__ = 'la_system_log_sms'
    __table_args__ = ({'comment': '系统短信日志表'})

    scene = Column(Integer, nullable=False, default=0, comment='场景编号')
    mobile = Column(String(32), nullable=False, default='', comment='手机号码')
    content = Column(String(255), nullable=False, default='', comment='发送内容')
    status = Column(Integer, nullable=False, default=0,comment='发送状态：[0=发送中, 1=发送成功, 2=发送失败]')
    results = Column(Text, nullable=True, comment='短信结果')
    send_time = Column(Integer, nullable=False, default=0, comment='发送时间')


system_config = SystemConfig.__table__
system_auth_admin = SystemAuthAdmin.__table__
system_auth_menu = SystemAuthMenu.__table__
system_auth_perm = SystemAuthPerm.__table__
system_auth_role = SystemAuthRole.__table__
system_log_login = SystemLogLogin.__table__
system_log_operate = SystemLogOperate.__table__
system_auth_post = SystemAuthPost.__table__
system_auth_dept = SystemAuthDept.__table__
system_log_sms = SystemLogSms.__table__



from sqlalchemy import event

def my_before_insert_listener(mapper, connection, target):
    print("------------------------------before insert -----------------------------")
    target.calculated_value = connection.execute(
        text("select my_special_function(%d)" % target.special_number)
    ).scalar()

# associate the listener function with SomeClass,
# to execute during the "before_insert" hook
event.listen(SystemAuthDept, 'before_insert', my_before_insert_listener)